package com.hrbu.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrbu.ssm.bean.Dept;
import com.hrbu.ssm.bean.DeptExample;
import com.hrbu.ssm.mapper.DeptMapper;
import com.hrbu.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Author: tanyajun
 * @CreateTime: 2021/3/25
 * @Blog: https://tanyajun.top
 */
@Service
@Transactional
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> getDeptList() {
        return deptMapper.selectByExample(null);
    }

    @Override
    public List<Dept> getDeptListByPage(Integer page, Integer limit) {
        Page<Dept> deptPage = PageHelper.startPage(page, limit);
        List<Dept> deptList = deptMapper.selectByExample(null);
        for (Dept dept : deptList) {
            System.out.println(dept);
        }
        return deptList;
    }

    @Override
    public Long getTotal() {
        return deptMapper.countByExample(null);
    }

    @Override
    public Integer insert(Dept dept) {
        if (dept == null || "".equals(dept.getName().trim())) {
            return 0;
        }
        return deptMapper.insertSelective(dept);
    }

    @Override
    public Integer update(Dept dept) {
        return deptMapper.updateByPrimaryKeySelective(dept);
    }

    @Override
    public Integer delete(Dept dept) {
        if (dept == null || dept.getId() == null || dept.getId() < 0) {
            return 0;
        }
        return deptMapper.deleteByPrimaryKey(dept.getId());
    }
}
